package com.evon.yardmanagement.dao;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.Restrictions;

import com.evon.yardmanagement.model.Vendor;

/**
 * A data access object (DAO) providing persistence and search support for
 * Company entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see com.abc.yardmanagement.dao.Company
 * @author MyEclipse Persistence Tools
 */

public class CompanyDAO extends BaseHibernateDAO<Vendor> {
	private static final Log log = LogFactory.getLog(CompanyDAO.class);
	// property constants
	public static final String COMPANY_NAME = "companyName";
	public static final String ADDRESS = "address";
	public static final String CITY = "city";
	public static final String PHONE = "phone";
	public static final String EMAIL = "email";
	public static final String HSC_ID = "hscId";
	public static final String HSU_ID = "hsuId";

	@Override
	public Class getQueryClass() {
		// TODO Auto-generated method stub
		return Vendor.class;
	}

	/**
	 * Return the list of vendors for the given country.
	 * 
	 * @param countryId
	 * @return
	 */
	public List<Vendor> getVendorsForCountry(int countryId) {
		Criteria criteria = getSession().createCriteria(getQueryClass())
				.createAlias("state", "state").createAlias("state.country",
						"country");
		criteria.add(Restrictions.eq("country.id", countryId));

		return criteria.list();
	}

	public List findByExample(Vendor instance) {
		log.debug("finding Company instance by example");
		try {
			List results = getSession().createCriteria(
					"com.evon.yardmanagement.model.Vendor").add(
					Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByCompanyName(Object companyName) {
		return findByProperty(COMPANY_NAME, companyName);
	}

	public List findByAddress(Object address) {
		return findByProperty(ADDRESS, address);
	}

	public List findByCity(Object city) {
		return findByProperty(CITY, city);
	}

	public List findByPhone(Object phone) {
		return findByProperty(PHONE, phone);
	}

	public List findByEmail(Object email) {
		return findByProperty(EMAIL, email);
	}

	public List findByHscId(Object hscId) {
		return findByProperty(HSC_ID, hscId);
	}

	public List findByHsuId(Object hsuId) {
		return findByProperty(HSU_ID, hsuId);
	}

}